package com.example.wyh.intelcheckbash.bug;

import android.content.ContentValues;
import android.content.Context;
import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.telephony.TelephonyManager;
import android.util.Log;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

import com.example.wyh.intelcheckbash.HttpUtil;
import com.example.wyh.intelcheckbash.LocalStorage;
import com.example.wyh.intelcheckbash.R;
import com.example.wyh.intelcheckbash.common.SysExitUtil;
import com.example.wyh.intelcheckbash.sqlite.TaskDbHelper;

import org.json.JSONException;
import org.json.JSONObject;

/**
 * Created by chen on 17/12/5.
 */

public class BugInfoActivity extends AppCompatActivity {

    TextView bug_info_machine_group;
    TextView bug_info_equipment_code;
    TextView bug_info_equipment_name;
    TextView bug_info_equipment_system;
    TextView bug_info_name;
    TextView bug_info_find_time;
    TextView bug_info_bug_level;
    EditText bug_info_appearance;
    TextView find_bug_info_department;
    TextView bug_info_find_owner;
    TextView bug_info_first_link_name;
    TextView bug_info_link_profession;
    TextView bug_info_link_time;
    EditText bug_info_remark_text;
    Button reupload_bug_btn;

    public String mDeviceToken;
    private String mYhxtm;

    public JSONObject mUploadData;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_bug_info);
        SysExitUtil.activityList.add(BugInfoActivity.this);
        getHttpHeads();
        initActionBar();
        try{
            mUploadData = new JSONObject(getIntent().getStringExtra("bugInfo"));
            showBugInfo(mUploadData);
        }catch (JSONException e)
        {

        }



        Log.e("=======bugInfo",getIntent().getStringExtra("bugInfo"));
    }
    public void showBugInfo(final JSONObject bugInfo)
    {
        try{
            bug_info_machine_group = (TextView)findViewById(R.id.bug_info_machine_group);
            bug_info_machine_group.setText(bugInfo.getString("jz"));

            bug_info_equipment_code = (TextView)findViewById(R.id.bug_info_equipment_code);
            bug_info_equipment_code.setText(bugInfo.getString("sbbm"));

            bug_info_equipment_name = (TextView)findViewById(R.id.bug_info_equipment_name);
            bug_info_equipment_name.setText(bugInfo.getString("sbmc"));

            bug_info_equipment_system = (TextView)findViewById(R.id.bug_info_equipment_system);
            bug_info_equipment_system.setText(bugInfo.getString("sbxt"));

            bug_info_name = (TextView)findViewById(R.id.bug_info_name);
            bug_info_name.setText(bugInfo.getString("qxmc"));

            bug_info_find_time = (TextView)findViewById(R.id.bug_info_find_time);
            bug_info_find_time.setText(bugInfo.getString("fxsj"));

            bug_info_bug_level = (TextView)findViewById(R.id.bug_info_bug_level);
            bug_info_bug_level.setText(bugInfo.getString("qxdj"));

            bug_info_appearance = (EditText)findViewById(R.id.bug_info_appearance);
            bug_info_appearance.setText(bugInfo.getString("qxxx"));
            bug_info_appearance.setEnabled(false);

            find_bug_info_department = (TextView)findViewById(R.id.find_bug_info_department);
            find_bug_info_department.setText(bugInfo.getString("fxbm"));

            bug_info_find_owner = (TextView)findViewById(R.id.bug_info_find_owner);
            bug_info_find_owner.setText(bugInfo.getString("fxr"));

            bug_info_first_link_name = (TextView)findViewById(R.id.bug_info_first_link_name);
            bug_info_first_link_name.setText(bugInfo.getString("lxr1"));

            bug_info_link_profession = (TextView)findViewById(R.id.bug_info_link_profession);
            bug_info_link_profession.setText(bugInfo.getString("lxzy1"));

            bug_info_link_time = (TextView)findViewById(R.id.bug_info_link_time);
            bug_info_link_time.setText(bugInfo.getString("lxsj1"));

            bug_info_remark_text = (EditText) findViewById(R.id.bug_info_remark_text);
            bug_info_remark_text.setText(bugInfo.getString("remark"));
            bug_info_remark_text.setEnabled(false);

            reupload_bug_btn = (Button)findViewById(R.id.reupload_bug_btn);
            reupload_bug_btn.setVisibility(View.VISIBLE);
            if (bugInfo.getString("isUpload").equals("1"))
            {
                reupload_bug_btn.setVisibility(View.GONE);
            }

            reupload_bug_btn.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    HttpUtil.requestNetForPostBugRecordData(mYhxtm, mDeviceToken, bugInfo, BugInfoActivity.this,"BugInfoActivity");
                }
            });

        }catch (JSONException e)
        {

        }
    }

    public void jumpToBugListActivity()
    {
        Intent data = new Intent();
        data.putExtra("uploadData", mUploadData.toString());
        BugInfoActivity.this.setResult(1002, data);
        BugInfoActivity.this.finish();
    }
    public void uploadBugFailed()
    {
        Handler mainHandler = new Handler(Looper.getMainLooper());
        mainHandler.post(new Runnable() {
            @Override
            public void run() {
                Toast.makeText(BugInfoActivity.this, "上传失败", Toast.LENGTH_SHORT).show();
                jumpToBugListActivity();
            }
        });
    }

    public void uploadBugSuccess(final JSONObject result ,final JSONObject uploadData)
    {
        final Handler mainHander = new Handler(Looper.getMainLooper());
        mainHander.post(new Runnable() {
            @Override
            public void run() {
                try {
                    TaskDbHelper taskDbHelper = new TaskDbHelper(BugInfoActivity.this);
                    SQLiteDatabase db = taskDbHelper.getWritableDatabase();
                    String qxxtm = uploadData.getString("qxxtm");
                    ContentValues cv = new ContentValues();

                    String status = result.getString("status");
                    if (status.equals("success"))
                    {
                        uploadData.put("isUpload","1");
                        cv.put("data",uploadData.toString());
                        int success = db.update("bug_list",cv,"user_id=? and qxxtm=?",new String[]{String.valueOf(LocalStorage.getUserXtm(BugInfoActivity.this)),
                                String.valueOf(qxxtm)});
                        if (success == 1) {
                            mUploadData.put("isUpload", "1");
                        }
                        Log.e("success",success+"");
                    }
                    db.close();
                    Toast.makeText(BugInfoActivity.this, "上传"+status, Toast.LENGTH_SHORT).show();
                }catch (JSONException e)
                {

                }
                jumpToBugListActivity();
            }
        });

    }

    public void getHttpHeads() {

        TelephonyManager TelephonyMgr = (TelephonyManager)getSystemService(TELEPHONY_SERVICE);
        mDeviceToken = TelephonyMgr.getDeviceId();
        JSONObject User = LocalStorage.getObject(BugInfoActivity.this, "User");
        mYhxtm = "";
        try {
            mYhxtm = User.getString("yhxtm");
        } catch (JSONException e) {
            throw new RuntimeException(e);
        }
    }

    public  void  initActionBar(){
        android.support.v7.app.ActionBar actionBar = getSupportActionBar();
        if (actionBar != null) {
            actionBar.setTitle("");
            actionBar.setDisplayShowHomeEnabled(false);
            actionBar.setDisplayShowCustomEnabled(true);
            LayoutInflater inflator = (LayoutInflater) this
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            View v = inflator.inflate(R.layout.actionbar_custom,new LinearLayout(BugInfoActivity.this),false);
            android.support.v7.app.ActionBar.LayoutParams layout = new android.support.v7.app.ActionBar.LayoutParams(
                    android.support.v7.app.ActionBar.LayoutParams.MATCH_PARENT, android.support.v7.app.ActionBar.LayoutParams.MATCH_PARENT);
            actionBar.setCustomView(v, layout);
            Toolbar parent = (Toolbar) v.getParent();
            parent.setContentInsetsAbsolute(0, 0);
        }
        TextView actionBarTitle = (TextView) findViewById(R.id.action_bar_title);
        actionBarTitle.setText("查看缺陷");
        ImageView title_logo = (ImageView)findViewById(R.id.title_logo_id);
        title_logo.setImageResource(R.drawable.quexian_title);
        title_logo.setAlpha(0.7f);
        RelativeLayout bar_back_btn =  (RelativeLayout) findViewById(R.id.bar_back_btn);
        bar_back_btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                BugInfoActivity.this.finish();
            }
        });
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
//            Intent data = new Intent();
//            data.putExtra("uploadData", mUploadData.toString());
//            this.setResult(1002, data);
//            this.finish();
        }
        return super.onKeyDown(keyCode, event);
    }
}
